package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.MoneyMap;
import com.qian.api.ddj.service.IMoneyMapService;
import com.qian.service.ddj.dao.MoneyMapRepository;
import com.qian.service.ddj.entity.DdjMoneyMap;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MoneyMapServiceImpl extends BaseServiceImpl<MoneyMap, DdjMoneyMap, Integer> implements IMoneyMapService {

    @Autowired
    private MoneyMapRepository dao;

    protected BaseRepository<DdjMoneyMap, Integer> getDao() {
        return dao;
    }

    protected DdjMoneyMap model2Entity(MoneyMap model) {
        DdjMoneyMap moneyMap = new DdjMoneyMap();
        moneyMap.setGroupId(model.getGroupId());
        moneyMap.setBeforeMoney(model.getBeforeMoney());
        moneyMap.setEndMoney(model.getEndMoney());
        moneyMap.setId(model.getId());
        return moneyMap;
    }

    protected MoneyMap entity2model(DdjMoneyMap entity) {
        MoneyMap moneyMap = new MoneyMap();
        moneyMap.setGroupId(entity.getGroupId());
        moneyMap.setBeforeMoney(entity.getBeforeMoney());
        moneyMap.setEndMoney(entity.getEndMoney());
        moneyMap.setId(entity.getId());
        return moneyMap;
    }

    @Override
    public MoneyMap save(MoneyMap model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            DdjMoneyMap ddjMoneyMap = dao.findOne(model.getId());
            if (isNotEmpty(model.getBeforeMoney()))
                ddjMoneyMap.setBeforeMoney(model.getBeforeMoney());
            if (isNotEmpty(model.getEndMoney()))
                ddjMoneyMap.setEndMoney(model.getEndMoney());
            model = entity2model(dao.save(ddjMoneyMap));
        }
        return model;
    }
}
